package com.block.storage.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.vo.ResponseVo;
import com.block.storage.config.OssAliConfig;
import com.block.storage.enums.BizTypeEnum;
import com.block.storage.enums.OssTypeEnum;
import com.block.storage.factory.OssFactory;
import com.block.storage.model.OssAuthInfoParam;
import com.block.storage.model.dto.OSSAuthInfoDTO;
import com.block.storage.model.vo.OssInfoVO;
import com.block.storage.service.IStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StorageServiceImpl implements IStorageService {

    @Autowired
    OssAliConfig ossAliConfig;

    @Override
    public ResponseVo getConfigInfo(String bizType) {

        try {
            OssInfoVO ossInfoVO = this.getOssInfo(bizType);

            return new ResponseVo(AppRspCodeConstant.SUCCESS, JSON.parseObject(JSON.toJSONString(ossInfoVO)));
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION);
    }


    @Override
    public OssInfoVO getOssInfo(String bizType) {

        try {
            BizTypeEnum entityByBizType = BizTypeEnum.getEntityByBizType(bizType);
            if (ObjectUtil.isEmpty(entityByBizType)){
                throw  new RuntimeException("业务类型错误");
            }

            OssAuthInfoParam ossAuthInfoParam = new OssAuthInfoParam();
            if (ObjectUtil.equals(entityByBizType.getPriFlag(), Integer.valueOf(1))){
                ossAuthInfoParam.setRegionId(ossAliConfig.getPriRegionId());
                ossAuthInfoParam.setBucketName(ossAliConfig.getPriBucketName());
                ossAuthInfoParam.setEndpoint(ossAliConfig.getPriEndpoint());
                ossAuthInfoParam.setHostUrl(ossAliConfig.getPriHostUrl());
            }else{
                ossAuthInfoParam.setRegionId(ossAliConfig.getRegionId());
                ossAuthInfoParam.setBucketName(ossAliConfig.getBucketName());
                ossAuthInfoParam.setEndpoint(ossAliConfig.getEndpoint());
                ossAuthInfoParam.setHostUrl(ossAliConfig.getHostUrl());
            }

            OSSAuthInfoDTO authParam = OssFactory.getService(OssTypeEnum.ALIYUN.getType()).getAuthParam(ossAuthInfoParam);

            OssInfoVO ossInfoVO = new OssInfoVO();
            ossInfoVO.setEndpoint(ossAuthInfoParam.getEndpoint());
            ossInfoVO.setBucketName(ossAuthInfoParam.getBucketName());
            ossInfoVO.setHostUrl(ossAuthInfoParam.getHostUrl());

            ossInfoVO.setPath(entityByBizType.getPath());
            ossInfoVO.setBizType(entityByBizType.getType());

            ossInfoVO.setAccessKeyId(authParam.getAccessKeyId());
            ossInfoVO.setAccessKeySecret(authParam.getAccessKeySecret());
            ossInfoVO.setSecurityToken(authParam.getSecurityToken());
            ossInfoVO.setExpiration(authParam.getExpiration());

            return ossInfoVO;
        }catch (Exception exception){
            exception.printStackTrace();
            throw  new RuntimeException("获取oss配置信息错误");
        }
    }

}
